package cn.lili.controller.im;


import cn.lili.modules.im.entity.ImAdminUser;
import cn.lili.modules.im.entity.ImMessage;
import cn.lili.modules.im.entity.ImUser;
import cn.lili.modules.im.service.ImAdminUserService;
import cn.lili.modules.im.service.ImMessageService;
import cn.lili.modules.im.service.ImUserService;
import cn.lili.security.AuthUser;
import cn.lili.security.UserContext;
import cn.lili.result.*;
import cn.lili.security.enums.UserEnums;
import cn.lili.security.token.Token;
import cn.lili.security.token.TokenUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Chopper
 */
@RestController
@Api(tags = "Im消息接口")
@RequestMapping("/lili/imUser")
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ImUserController {

    private final ImUserService imUserService;

    private final ImAdminUserService imAdminUserService;

    private final ImMessageService imMessageService;
    @Autowired
    private TokenUtil tokenUtil;

    @GetMapping("/get/{userId}/{type}")
    @ApiOperation(value = "获取用户信息")
    public ResultMessage<Object> get(@PathVariable("userId") String userId,@PathVariable("type") String type) {
        String name="";
        String face="";
        Map<String, Object> map = Maps.newHashMap();
        if("admin".equals(type)){
            ImAdminUser imAdminUser = imAdminUserService.getById(userId);
            name=imAdminUser.getName();
            face=imAdminUser.getFace();
            map.put("imUser",imAdminUser);
        }else{
            ImUser imUser = imUserService.getById(userId);
            name=imUser.getName();
            face=imUser.getFace();
            map.put("imUser",imUser);
        }
        AuthUser authUser = AuthUser.builder()
                .username(name)
                .face(face)
                .id(userId)
                .nickName(name)
                .longTerm(true)
                .build();
        Token token = tokenUtil.createToken(authUser);
        map.put("imToken",token);
        return ResultUtil.data(map);
    }

    @GetMapping("/getNum/{userId}")
    @ApiOperation(value = "获取用户信息")
    public ResultMessage<Object> getNum(@PathVariable("userId") String userId) {
        LambdaQueryWrapper<ImMessage> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ImMessage::getToUser,userId);
        queryWrapper1.eq(ImMessage::getIsRead, false);
        queryWrapper1.orderByDesc(ImMessage::getCreateTime);
        List<ImMessage> list = imMessageService.list(queryWrapper1);
        return ResultUtil.data(list.size());
    }

}
